package com.StarStudio.Service.Impl;


import com.StarStudio.DAO.PanicBuyingCouponsDAO;
import com.StarStudio.Service.CouponsService;
import com.StarStudio.Service.PanicBuyingCouponsService;
import com.ruoyi.common.core.Entity.Coupons;
import com.ruoyi.common.core.Entity.PanicBuyingCoupons;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class PanicBuyingCouponsServiceImpl implements PanicBuyingCouponsService {

    @Autowired
    PanicBuyingCouponsDAO panicBuyingCouponsDAO;

    @Autowired
    CouponsService couponsService;


    public List<PanicBuyingCoupons> findPanicBuyingCouponsList(PanicBuyingCoupons panicBuyingCoupons) {
        return panicBuyingCouponsDAO.findPanicBuyingCouponsList(panicBuyingCoupons);
    }

    @Override
    public long findTotalRecord() {
        return panicBuyingCouponsDAO.findTotalRecord();
    }

    @Override
    public PanicBuyingCoupons findByCoupons_id(long coupons_id) {
        return panicBuyingCouponsDAO.findByCoupons_id(coupons_id);
    }

    @Override
    public List<PanicBuyingCoupons> PanicBuyingCouponsList() {
        return panicBuyingCouponsDAO.PanicBuyingCouponsList();
    }

    @Override
    public int updatePanicBuyingCoupons(PanicBuyingCoupons panicBuyingCoupons) {
        int new_pb_coupons_stock = panicBuyingCoupons.getPanic_buying_coupons_stock();
        int old_pb_coupons_stock = panicBuyingCouponsDAO.findPanic_buying_coupons_stockByPanic_buying_coupons_id(panicBuyingCoupons.getPanic_buying_coupons_id());
        if (new_pb_coupons_stock != old_pb_coupons_stock){
            Coupons byCoupon_id = couponsService.findByCoupon_id(panicBuyingCoupons.getCoupons_id());
            int totalStock = byCoupon_id.getStock() + old_pb_coupons_stock;
            if (new_pb_coupons_stock > old_pb_coupons_stock){ /* 抢购优惠券库存增加 */
                if (new_pb_coupons_stock <= totalStock){
                    byCoupon_id.setStock(totalStock - new_pb_coupons_stock);
                }else {
                    throw new RuntimeException("优惠券库存不足！");
                }
            } else { /* 抢购优惠券库存减少 */
                if (new_pb_coupons_stock >= 0){
                    byCoupon_id.setStock(totalStock - new_pb_coupons_stock);
                }else {
                    throw new RuntimeException("库存不能小于零！");
                }
            }
            couponsService.updateCoupons(byCoupon_id);
        }
        return panicBuyingCouponsDAO.updatePanicBuyingCoupons(panicBuyingCoupons);
    }

    @Override
    public void AddPanicBuyingCoupons(PanicBuyingCoupons panicBuyingCoupons) {
        Coupons byCoupon_id = couponsService.findByCoupon_id(panicBuyingCoupons.getCoupons_id());
        if (byCoupon_id.getStock() >= panicBuyingCoupons.getPanic_buying_coupons_stock()){
            byCoupon_id.setStock(byCoupon_id.getStock() - panicBuyingCoupons.getPanic_buying_coupons_stock());
            couponsService.updateCoupons(byCoupon_id);
            panicBuyingCouponsDAO.AddPanicBuyingCoupons(panicBuyingCoupons);
        }else {
            throw new RuntimeException("优惠券库存不足！");
        }
    }

    @Override
    public void DeletePanicBuyingCoupons(long panic_buying_coupons_id) {
        //删除抢购优惠券时，把库存归还到对应的普通优惠券库存中
        int pbc_stock = panicBuyingCouponsDAO.findPanic_buying_coupons_stockByPanic_buying_coupons_id(panic_buying_coupons_id);
        long couponsId = panicBuyingCouponsDAO.findCouponsIdByPBCId(panic_buying_coupons_id);
        int OldStock = couponsService.findStockByCouponsId(couponsId);
        couponsService.updateStockById(couponsId,OldStock + pbc_stock);
        //逻辑删除抢购优惠券
        panicBuyingCouponsDAO.DeletePanicBuyingCoupons(panic_buying_coupons_id);
    }

    @Override
    public List<PanicBuyingCoupons> findByStatus(int status) {
        return panicBuyingCouponsDAO.findByStatus(status);
    }

    /**
     * 每分钟更新抢购优惠券状态
     */
    @Scheduled(cron = "0 */1 * * * ?")
    public void AutoUpdatePanicBuyingStatus(){
        List<PanicBuyingCoupons> panicBuyingCouponsList = panicBuyingCouponsDAO.PanicBuyingCouponsList();
        if (!CollectionUtils.isEmpty(panicBuyingCouponsList)){
            for (PanicBuyingCoupons panicBuyingCoupons : panicBuyingCouponsList) {
                if (new Timestamp(new Date().getTime()).before(panicBuyingCoupons.getPanic_buying_start_time())){
                    panicBuyingCoupons.setPanic_buying_status(0);
                }else if (new Timestamp(new Date().getTime()).after(panicBuyingCoupons.getPanic_buying_end_time())){
                    panicBuyingCoupons.setPanic_buying_status(2);
                }else {
                    panicBuyingCoupons.setPanic_buying_status(1);
                }
                panicBuyingCouponsDAO.updatePanicBuyingCoupons(panicBuyingCoupons);
            }
        }
    }
}
